<link type="text/css" href="/admins/js/pagerView/pager-view.css" rel="stylesheet" />
<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/jquery/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/admins/js/jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="/admins/js/pagerView/pager-view.js"></script>
<script type="text/javascript" src="/admins/js/fileUpload/ajaxfileupload.js"></script>
<script type="text/javascript">
$(function(){
	$.datepicker.setDefaults( $.datepicker.regional[ "zh-CN" ] );
	$( "#del-button").button().click( function () {
		if (confirm( "你真的要删除选中项吗？" )) {
			idList = "";
			sp = "";
			$( ".checkbox-del" ).each(function(){
				if ($(this).attr("checked") == "checked") {
					idList += sp + $(this).val();
					if (sp == "") sp = ",";
				}
			});
			$.ajax({
				type: "POST",
				url: "/admin/order/ingstore-del",
				dataType: "json",
				data:"idList="+idList+"&comefrom="+$( "#comefrom" ).val(),
				success: function(data) {
					var result=eval(data);
					if (result == true) {
						$( "#queryButton").click();
						alert( "删除成功！" );
					} else {
						alert( "删除失败！" );
					}
				}
			});
		}
	});
    $( "#queryButton").button().click( function () {
		$("#starttime2").val($("#starttime").val());$("#endtime2").val($("#endtime").val());
		$("#logisticsNo2").val($("#logisticsNo").val());$("#username2").val($("#username").val());
		$("#state2").val($("#state").val());
	    $.ajax({
			type: "POST",
			url: "/admin/order/ingstore-query",
			dataType: "json",
			data:"starttime="+$("#starttime").val()+"&endtime="+$("#endtime").val()+"&logisticsNo="+$("#logisticsNo").val()+"&username="+$("#username").val()+"&state="+$("#state").val()+"&pagesize="+pager.size,
			success: function(data) {
				var items=eval(data.items);
				bindDataTable(items)
				pager.itemCount = data.count;
				pager.index = 1
				pager.render();
			}
		});

		return false; 
	});
	$( "#starttime" ).datepicker();
	$( "#endtime" ).datepicker();
	var pager = new PagerView('pager');
	pager.size = 15; // 每页显示15条记录
	pager.maxButtons = 10;
	pager.itemCount = 0;
	pager.onclick = function(index) {
		$.ajax({
			type: "POST",
			url: "/admin/order/ingstore-pager",
			dataType: "json",
			data:"starttime="+$("#starttime2").val()+"&endtime="+$("#endtime2").val()+"&logisticsNo="+$("#logisticsNo2").val()+"&username="+$("#username2").val()+"&state="+$("#state2").val()+"&comefrom="+$("#comefrom").val()+"&pagesize="+pager.size+"&pageindex="+index,
			success: function(data) {
				var items=eval(data);
				bindDataTable(items)
			}
		});
		
	};
	pager.render();

	$( "#queryButton").click();
});
function bindDataTable(items) {
	$(".datatable tbody").empty();
	$.each(items, function(k)
	{
		switch (items[k]['state']) {
		    case '0':
			    items[k]['state'] = '未付款';
				break;
			case '1':
			    items[k]['state'] = '已付款';
				break;
			case '2':
			    items[k]['state'] = '已受理';
				break;
			case '3':
			    items[k]['state'] = '已发贷';
				break;
			case '4':
			    items[k]['state'] = '已入库';
				break;
			case '5':
			    items[k]['state'] = '已出库';
				break;
			default:
			    items[k]['state'] = '未知';
				break;
		} 
		$(".datatable tbody").append("<tr><td><input type=\"checkbox\" value=\""+items[k]['id']+"\" class=\"checkbox-del\" /></td><td>"+items[k]['id']+"</td><td>"+items[k]['shipno']+"</td><td>"+items[k]['typeText']+"</td><td>"+items[k]['source']+"</td><td>"+items[k]['productName']+"</td><td>"+items[k]['shipType']+"</td><td>"+items[k]['remark']+"</td><td>"+items[k]['state']+"</td><td>"+items[k]['quantity']+"</td><td><a href=\"javascript:void(0)\" id=\""+items[k]['id']+"\" class=\"edit-operation\">验货入库</a></td></tr>");	
	});
	if (items.length > 0 && items[0]['comefrom'] == '1') {
		$( "#comefrom" ).val('1');
	} else {
		$( "#comefrom" ).val('2');
	}
	$( ".edit-operation").click( function () {
		showDialog(this.id);
	}).css('color','#333');
	$( ".datatable tbody tr" ).each(function(){
		$(this).click( function () { 
			$( ".datatable .select" ).removeClass("select");
			$(this).addClass("select"); 
		});
		$(this).hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		);
	});
}
</script>
<form action="/admin/order/ingstore" method="post" style="margin:0px">
<div style=" border:1px solid #DDDDDD; padding:2px; margin:2px;">
<label for="starttime">时间：</label><input type="text" id="starttime" name="starttime" />
<label for="endtime">--</label><input type="text" id="endtime" name="endtime" />

<label for="logisticsNo">物流单号：</label><input type="text" id="logisticsNo" name="logisticsNo" />
<label for="username">用户：</label><input type="text" id="username" name="username" />
<label for="state">状态：</label>
<select id="state" name="state">
  <option value="">全部</option>
  <option value="0">未付款</option>
  <option value="1">已付款</option>
  <option value="2">已受理</option>
  <option value="3">已发贷</option>
  <option value="4">已入库</option>
  <option value="5">已出库</option>
</select>
<input type="submit" id="queryButton" value="查询"/>
</div>
</form>
<style>
	.datatable {
		text-align: left;
		font-size: 12px;
		font-family: verdana;
		background: #c0c0c0; margin:2px;
	}

	.datatable thead tr,

	.datatable tfoot tr {
		background: #c0c0c0;
	}

	.datatable tbody tr {
		background: #f0f0f0;
	}

	.datatable td, th {
		border: 1px solid white; height:28px;
	}
	.datatable td {background-color:white;}
	
	.datatable .hover td {background-color:#99CCFF;}
	
	.datatable .select td {background-color:#EEEE99;}

	#ui-datepicker-div{ font-size:12px;}
</style>
<div style="padding:2px; margin:2px;">
<table cellspacing="1" width="100%" class="datatable">
	<thead>
		<tr>
            <th></th>
			<th>ID</th>
			<th>物流单号</th>
			<th>类型</th>
			<th>来源</th>
			<th>包裹名称</th>
			<th>快递公司</th>
			<th>货物备注</th>
            <th>状态</th>
            <th>入库数量</th>
            <th>操作</th>
		</tr>
	</thead>
    <tfoot>
      <tr><td colspan="14"><button id="del-button">删除选中</button></td></tr>
    </tfoot>
	<tbody>
	</tbody>
</table>
</div>
<form action="/admin/order/ingstore-pager" method="post">
<input type="hidden" id="starttime2" name="starttime2" value="" />
<input type="hidden" id="endtime2" name="endtime2" value="" />
<input type="hidden" id="logisticsNo2" name="logisticsNo2" value="" />
<input type="hidden" id="username2" name="username2" value="" />
<input type="hidden" id="state2" name="state2" value="" />
<input type="hidden" id="comefrom" name="comefrom" value="" />
</form>
<div id="pager" class="pager"></div>
<style type="text/css">
#dialog-modal { display:none; }
#dialog-modal input { width:170px;}
</style>
<script type="text/javascript">
$(function(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-modal" ).dialog({
		height: 380,
		width: 600,
		modal: true,
		autoOpen: false
	});
    $( "#edit-button-ok").button().click( function () {
		id = $("#record-id").val();op_name = $("#op-name").val();op_type = $("#op-type").val();
		op_source = $("#op-source").val();op_logistics = $("#op-logistics").val();op_date = $("#op-date").val();
		op_remark = $("#op-remark").val();op_img = op_weight = op_quantity = '';
		sp = '';
		$( ".op-img" ).each(function(index, element) {
			op_img += sp + $( ".op-img" ).eq(index).val();
			op_weight += sp + $( ".op-weight" ).eq(index).val();
			op_quantity += sp + $( ".op-quantity" ).eq(index).val();
			if (sp == '') sp = ',';
			$.ajaxFileUpload
			(
				{
					url:'/admin/order/ingstore-file?storeId='+id,
					secureuri:false,
					fileElementId:element.id,
					dataType: 'json',
					data:{name:'logan', id:'id'},
					success: function (data, status)
					{
						if (data == true) {
							
						} 
					},
					error: function (data, status, e)
					{
						alert(e);
					}
				}
			)
			
        });
		
		$.ajax({
			type: "POST",
			url: "/admin/order/ingstore-edit",
			dataType: "json",
			data:"id="+id+"&op_name="+op_name+"&op_type="+op_type+"&op_source="+op_source+"&op_logistics="+op_logistics+"&op_date="+op_date+"&op_remark="+op_remark+"&op_img="+op_img+"&op_weight="+op_weight+"&op_quantity="+op_quantity,
			success: function(data) {
				var result=eval(data);
				if (result == true) {
					//row = $( ".datatable .select" ).children();
//					$(row).eq(3).text(productName);
//					$(row).eq(4).text(state);
//					$(row).eq(5).text(storeId_value);
//					$(row).eq(6).text(dateInStore_value);
//					$(row).eq(7).text(price_value);
//					$(row).eq(8).text(quantity_value);
//					$(row).eq(9).text(weight_value);
//					$(row).eq(10).text(orderNo_value);
//					$(row).eq(12).text(createdate_value);
                    alert( "更新完成！" );
                    $( "#dialog-modal" ).dialog( "close" );
				} else {
					alert( "更新失败！" );
				}
			}
		});
		
		
	});
	$( "#edit-button-cancel").button().click( function () {
		$( "#dialog-modal" ).dialog( "close" );
	});
});
function showDialog(id) {
	$.ajax({
		type: "POST",
		url: "/admin/order/ingstore-details",
		dataType: "json",
		data:"id="+id+"&comefrom="+$( "#comefrom" ).val(),
		success: function(data) {
			var object=eval(data);
			if (object != false) {
			    $("#record-id").val( object.id );
				$("#op-name").val( object.productName );
				$("#op-type").val( object.typeText );
				$("#op-source").val( object.source );
				$("#op-logistics").val( object.shipType );
				$("#op-date").val( object.createtime );
				$("#op-remark").val( object.remark );
				html = '';
				for(i=1;i<=object.quantity;i++) {
					html += '<tr><td align="right">商品'+i+'图片：</td><td colspan="3"><input type="file" id="op-img'+i+'" name="op-img'+i+'" class="op-img" style="width:99%"/></td></tr><tr><td align="right">商品'+i+'重量：</td><td><input id="op-weigh'+i+'t" class="op-weight" type="text" value="" /></td><td align="right">商品'+i+'数量：</td><td><input id="op-quantity'+i+'" class="op-quantity" type="text" value="1" readonly="readonly" style="background-color:#CCCCCC"/></td></tr>';
				}
				$( "#op-piclist" ).empty().html(html);
				$( "#dialog-modal" ).dialog({ title: "物流单号："+object.logistics+"　"+object.quantity+"个商品" });	
				$( "#dialog-modal" ).dialog( "open" );
			} else {
				alert( "获取数据失败！" );
			}
		}
	});
}
</script>
<div id="dialog-modal" title="操作">
	<table width="100%" border="0">
      <tr>        
        <td align="right">包裹名称：</td><td colspan="3"><input id="op-name" type="text" value="" readonly="readonly"  style="width:99%;background-color:#CCCCCC"/></td> 
     </tr>
     <tr>
		<td align="right">类型：</td><td><input id="op-type" type="text" value="" readonly="readonly" style="background-color:#CCCCCC" /></td>
		<td align="right">来源：</td><td><input id="op-source" type="text" value="" readonly="readonly" style="background-color:#CCCCCC" /></td>
     </tr>
     <tr>
		<td align="right">快递公司：</td><td><input id="op-logistics" type="text" value="" readonly="readonly" style="background-color:#CCCCCC" /></td>
		<td align="right">到货时间：</td><td><input id="op-date" type="text" value="" readonly="readonly" style="background-color:#CCCCCC" /></td>
     </tr>
     <tr>
        <td align="right">商品备注：</td><td colspan="5"><textarea id="op-remark" rows="3" style="width:99%" readonly="readonly" style="background-color:#CCCCCC" ></textarea></td>
     </tr>
     <tr>
       <td colspan="6">
       <table width="100%" id="op-piclist"></table>
       </td>
     </tr>
     <tr>
       <td colspan="6" align="center">
       <button id="edit-button-ok">确认保存</button>
       <button id="edit-button-cancel">取消返回</button><input type="hidden" id="record-id" value="" />
       </td>
     </tr>
    </table>
</div>